
# 入环的位置
"""
环前 a
环   b
快指针 f
慢指针 s
f = 2s
f - s = nb  相遇时，快指针比慢指针多走n圈
s = nb
f = 2nb

# 走到起点
现在 s 走到 环入口 nb + a 一定在环入口
"""


class Solution:

    def detectCycle(self, head):
        slow = fast = head
        while fast and fast.next:

            # pass
            slow = slow.next
            fast = fast.next.next
            
            # 表明存在环
            if slow is fast:
                while slow is not head:
                    slow = slow.next
                    head = head.next
                
                return head
        return None

